package com.erp.erp_servers.Inquiry.imp;

import com.erp.erp_dao.Inquiry.IInquiryProductDAO;
import com.erp.erp_dao.Inquiry.IInquiryRecordDAO;
import com.erp.erp_entitys.Inquiry.InquiryProductEntity;
import com.erp.erp_entitys.Inquiry.InquiryRecordEntity;
import com.erp.erp_servers.Inquiry.IInquiryRecordService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;

@Service
public class ImpInquiryRecordService implements IInquiryRecordService {
    @Autowired
    private IInquiryRecordDAO inquiryRecordDAO;
    @Autowired
    private IInquiryProductDAO inquiryProductDAO;
    @Override
    public int deleteByPrimaryKey(Integer id) {
        return inquiryRecordDAO.deleteByPrimaryKey(id);
    }

    @Override
    public int insert(InquiryRecordEntity record) {
        return inquiryRecordDAO.insert(record);
    }

    @Override
    public int insertSelective(InquiryRecordEntity record) {
        return inquiryRecordDAO.insertSelective(record);
    }

    @Override
    public InquiryRecordEntity selectByPrimaryKey(Integer id) {
        return inquiryRecordDAO.selectByPrimaryKey(id);
    }

    @Override
    public Map<Integer,List<InquiryRecordEntity>> selectRecordByInId(Integer inId) {
        Map<Integer,List<InquiryRecordEntity>> map = new LinkedHashMap<>();

        List<InquiryRecordEntity> dbList = inquiryRecordDAO.selectRecordByInId(inId, 0);
        for(int i = 0; i<dbList.size();i++){
            List<InquiryRecordEntity> list = new ArrayList<>();
            InquiryRecordEntity record = dbList.get(i);
            Integer frequency = record.getFrequency();
            for(int j = 0; j<dbList.size();j++){
                InquiryRecordEntity next = dbList.get(j);
                if(frequency==next.getFrequency()){
                    list.add(next);
                }
            }
            if(list.size()>0){
                map.put(frequency,list);
            }
        }
        return map;
    }

    @Override
    public boolean setMasterRecordByInId(Integer inId, Integer frequency) {
        boolean flag = false;
//        List<InquiryProductEntity> dbProList = inquiryProductDAO.selectProductByInId(inId);
        List<InquiryRecordEntity> recordList = inquiryRecordDAO.selectRecordByInId(inId,frequency);
//        if (CollectionUtils.isEmpty(dbProList))
//        if(dbProList.size()<recordList.size()){
//            for (InquiryRecordEntity record : recordList){
//                for (InquiryProductEntity proDb : dbProList){
//                    if(proDb.getId().equals(record.getProductId())){
//                        InquiryProductEntity pro = new InquiryProductEntity();
//                        BeanUtils.copyProperties(proDb, pro);
//                        BeanUtils.copyProperties(record, pro);
//                        pro.setId(proDb.getId());
//                        flag = inquiryProductDAO.updateByPrimaryKey(pro)>0?true:false;
//                    }
//                }
//            }
//        }
        for (InquiryRecordEntity record : recordList){
            if (record!=null){
                Integer proId = record.getProductId();
                if (proId!=null){
                    InquiryProductEntity proDb = inquiryProductDAO.selectByPrimaryKey(proId);
                    if(proDb!=null){
                        InquiryProductEntity pro = new InquiryProductEntity();
                        BeanUtils.copyProperties(proDb, pro);
                        BeanUtils.copyProperties(record, pro);
                        pro.setId(proDb.getId());
                        flag = inquiryProductDAO.updateByPrimaryKey(pro)>0?true:false;
                    }
                }
            }
        }
        return flag;
    }
}
